package com.trailwalker.javafx.widget.dialog;

import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Timer;
import java.util.TimerTask;

public class JfxToast {

    public static final int LENGTH_LONG=3000;
    public static final int LENGTH_SHORT=2000;

    private Stage primaryStage=null;
    private Stage toastStage=null;
    private String text="";
    private int time=2000;
    private final double primaryStageHeight;
    private final double primaryStageWidth;
    private final Label lblToast;
    private final double lblToastHeight;
    //private final double lblToastWidth;

    private JfxToast(Stage primaryStage, String text, int time) {
        this.primaryStage=primaryStage;
        this.text=text;
        this.time=time;

        toastStage=new Stage();
        toastStage.initStyle(StageStyle.TRANSPARENT);
        primaryStageHeight = primaryStage.getHeight();
        primaryStageWidth = primaryStage.getWidth();

        //默认信息
        lblToast = new Label(this.text);
        lblToastHeight = primaryStageHeight /10;
        double radius= lblToastHeight /2;
        lblToast.setBackground(new Background(new BackgroundFill(Paint.valueOf("#000000bb"),new CornerRadii(radius), Insets.EMPTY)));
        lblToast.setTextFill(Color.WHITE);//消息字体颜色
        lblToast.setPrefHeight(lblToastHeight);
        lblToast.setPadding(new Insets(lblToastHeight /4));
        lblToast.setAlignment(Pos.CENTER);//居中
        lblToast.setFont(new Font((int)(lblToastHeight /4)));//字体大小

//        java.awt.Font font = new java.awt.Font("微软雅黑", java.awt.Font.PLAIN,(int)(lblToastHeight /4));
//        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
//        int txtW = metrics.stringWidth(this.text);//文字的宽度
//        lblToastWidth = txtW+lblToastHeight/2;

        HBox hblblContainer = new HBox();
        hblblContainer.getChildren().add(lblToast);
        hblblContainer.setAlignment(Pos.CENTER);
        hblblContainer.setBackground(new Background(new BackgroundFill(Paint.valueOf("#00000000"),CornerRadii.EMPTY, Insets.EMPTY)));

        Scene scene=new Scene(hblblContainer);
        scene.setFill(null);//场景透明
        toastStage.setScene(scene);
        toastStage.setWidth(primaryStageWidth);
        toastStage.setHeight(lblToastHeight);
        toastStage.setAlwaysOnTop(true);
    }

    public static JfxToast makeText(Stage primaryStage, String text, int time){
        return new JfxToast(primaryStage,text,time);
    }

    public void show(){
        TimerTask task= new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(()->toastStage.close());
            }
        };
        Timer timer=new Timer();
        timer.schedule(task,time);
        //toastStage.setX(primaryStage.getX()+primaryStageWidth/2-lblToastWidth/2);
        toastStage.setX(primaryStage.getX());
        toastStage.setY(primaryStage.getY()+primaryStageHeight/4*3-lblToastHeight/2);
        toastStage.show();
    }

}
